package com.zichen.pr.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zichen.pr.common.constant.UserConstants;
import com.zichen.pr.common.core.controller.BaseController;
import com.zichen.pr.common.core.domain.entity.SysDictType;
import com.zichen.pr.common.core.domain.result.Result;
import com.zichen.pr.common.core.page.TableDataInfo;
import com.zichen.pr.system.service.SysDictTypeService;
import com.zichen.pr.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @program: pr
 * @description: 字典 控制层
 * @author: rzhang
 * @create: 2021-04-01 16:51
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Resource(name = "sysDictTypeService")
    private SysDictTypeService sysDictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictType dictType) {
        startPage();
        List<SysDictType> list = sysDictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public Result getInfo(@PathVariable Long dictId) {
        return Result.success(sysDictTypeService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping
    public Result add(@Validated @RequestBody SysDictType dict) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dictType", dict.getDictType());
        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.count(wrapper))) {
            return Result.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysDictTypeService.save(dict));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping
    public Result edit(@Validated @RequestBody SysDictType dict) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dictType", dict.getDictType());
        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.count(wrapper))) {
            return Result.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysDictTypeService.updateById(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/{dictIds}")
    public Result remove(@PathVariable Long[] dictIds) {
        return toAjax(sysDictTypeService.removeByIds(Arrays.asList(dictIds)));
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public Result optionselect() {
        List<SysDictType> dictTypes = sysDictTypeService.selectDictTypeAll();
        return Result.success(dictTypes);
    }
}
